package com.example.permission.service.impl;

import com.alibaba.fastjson.JSON;
import com.example.permission.common.CommonParam;
import com.example.permission.common.page.Page;
import com.example.permission.entity.dto.SysMenuDTO;
import com.example.permission.entity.dto.page.SysMenuPageDTO;
import com.example.permission.entity.po.SysMenu;
import com.example.permission.entity.vo.SysMenuTreeVO;
import com.example.permission.mapper.SysMenuMapper;
import com.example.permission.service.SysMenuService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 权限功能(SysMenu)表服务实现类
 *
 * @author xuwq
 * @date 2020-05-09 15:56:07
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysMenuServiceImpl implements SysMenuService {

    @Autowired
    private SysMenuMapper sysMenuMapper;


    @Override
    public SysMenu insert(SysMenuDTO sysMenuDTO) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO, sysMenu);
        sysMenu.setCreateTime(new Date());
        sysMenu.setCreator(CommonParam.getInstance().getUserNo());
        this.sysMenuMapper.insert(sysMenu);
        return sysMenu;
    }

    @Override
    public SysMenu update(SysMenuDTO sysMenuDTO) {
        SysMenu sysMenu = new SysMenu();
        BeanUtils.copyProperties(sysMenuDTO, sysMenu);
        sysMenu.setUpdateTime(new Date());
        sysMenu.setUpdator(CommonParam.getInstance().getUserNo());
        this.sysMenuMapper.update(sysMenu);
        return this.queryById(sysMenu.getMenuId());
    }

    @Override
    public boolean deleteById(Integer menuId) {
        return this.sysMenuMapper.deleteById(menuId) > 0;
    }

    @Override
    public SysMenu queryById(Integer menuId) {
        return this.sysMenuMapper.queryById(menuId);
    }

    @Override
    public Page<SysMenu> queryListByPage(SysMenuPageDTO sysMenuPageDTO) {
        int count = this.sysMenuMapper.queryCountByPage(sysMenuPageDTO);
        List<SysMenu> sysMenuList = this.sysMenuMapper.queryListByPage(sysMenuPageDTO);
        return new Page<>(count, sysMenuList);
    }

    /**
     * 递归树
     *
     * @param sysMenuTreeVOList
     * @param pId
     * @return
     */
    private static List<SysMenuTreeVO> makeTree(List<SysMenuTreeVO> sysMenuTreeVOList, Integer pId) {
        //子类
        List<SysMenuTreeVO> children = sysMenuTreeVOList.stream()
                .filter(x -> x.getPid().equals(pId)).collect(Collectors.toList());
        //后辈中的非子类
        List<SysMenuTreeVO> successor = sysMenuTreeVOList.stream()
                .filter(x -> !x.getPid().equals(pId)).collect(Collectors.toList());
        children.forEach(x -> makeTree(successor, x.getMenuId()).forEach(y -> x.getChildren().add(y)));
        return children;
    }

    @Override
    public List<SysMenu> queryDeptTree(Integer pid) {
        SysMenu sysMenu = new SysMenu();
        sysMenu.setPid(pid);
        return sysMenuMapper.queryAll(sysMenu);
    }

    @Override
    public List<SysMenuTreeVO> queryDeptTree() {
        List<SysMenu> sysMenuList = sysMenuMapper.queryAll(null);
        List<SysMenuTreeVO> sysMenuTreeVOList = JSON.parseArray(JSON.toJSONString(sysMenuList), SysMenuTreeVO.class);
        return makeTree(sysMenuTreeVOList, -1);
    }

}